package edu.zust.zw.shop.controller;

import java.io.Serializable;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import edu.zust.zw.shop.db.dao.domain.ShopUserDomain;
import edu.zust.zw.shop.util.Result;



/**
 * 
 * @author psh
 *
 */
public class BaseController {

	/**
	 * 得到request对象
	 * 
	 * @author psh
	 */
	protected HttpServletRequest getRequest() {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
				.getRequest();
		return request;
	}

	/**
	 * 获得response对象
	 * 
	 * @return response
	 * @author psh
	 */
	protected HttpServletResponse getResponse() {
		HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
				.getResponse();
		return response;
	}

	/**
	 * 获得session
	 * 
	 * @param request
	 * @return
	 * @author psh
	 */
	protected HttpSession getSession(HttpServletRequest request) {
		return request.getSession();
	}

	/**
	 * 获得sessionId
	 * 
	 * @param request
	 * @return sessionId
	 * @author psh
	 */
	protected String getSessionId(HttpServletRequest request) {
		// request.getRequestedSessionId();
		// //Http://localhost:8080/hdj/xxx.do?JESSIONID=ewrqwrq234123412
		return request.getSession().getId();
	}

	/**
	 * 拿到当前session 设置session中的值
	 * 
	 * @param request
	 * @param name
	 * @param value
	 * @author psh
	 */
	protected void setSessionAttribute(HttpServletRequest request, String name, Serializable value) {
		HttpSession session = request.getSession();// true Cookie JSESSIONID
		session.setAttribute(name, value);
	}

	/** 基于@ExceptionHandler异常处理 */
	@ExceptionHandler
	@ResponseBody
	public Result exp(HttpServletRequest request, Exception ex) {
		ex.printStackTrace();
		if (ex instanceof MissingServletRequestParameterException) {
			return Result.error(Result.Code.PARAMETER_ERROR);
		}
		return Result.error(Result.Code.SYSTEM_ERROR);
	}

	protected Result getResponseSuccess() {
		return Result.success();
	}

	protected Result getNoUpdate() {
		return Result.error(Result.Code.NO_UPDATE);
	}

	protected Result getResponseSysError() {
		return Result.error(Result.Code.PARAMETER_ERROR);
	}

	protected Result getParmemeterError() {
		return Result.error(Result.Code.PARAMETER_ERROR);
	}

	protected String getPictureCode() {
		return getSession(getRequest()).getAttribute("pictureCode").toString();
	}

	protected String getMessageCode() {
		return getSession(getRequest()).getAttribute("messageCode").toString();
	}

	protected void addPictureCode(String code) {
		getSession(getRequest()).setAttribute("pictureCode", code);
	}

	protected void addMessageCode(String code) {
		getSession(getRequest()).setAttribute("messageCode", code);
	}

	protected ShopUserDomain getUserInfo() {
		Object object = getSession(getRequest()).getAttribute("userInfo");
		if (object != null) {
			return (ShopUserDomain) object;
		}
		return null;
	}
}
